home *** CD-ROM | disk | FTP | other *** search
- Imports System.IO
- Imports System.Text.RegularExpressions
-
- Public Class Form1
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents Label1 As System.Windows.Forms.Label
- Friend WithEvents Label2 As System.Windows.Forms.Label
- Friend WithEvents Label3 As System.Windows.Forms.Label
- Friend WithEvents Label4 As System.Windows.Forms.Label
- Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
- Friend WithEvents txtSource As System.Windows.Forms.TextBox
- Friend WithEvents txtRegex As System.Windows.Forms.TextBox
- Friend WithEvents btnLoad As System.Windows.Forms.Button
- Friend WithEvents btnFind As System.Windows.Forms.Button
- Friend WithEvents btnSplit As System.Windows.Forms.Button
- Friend WithEvents btnReplace As System.Windows.Forms.Button
- Friend WithEvents txtReplace As System.Windows.Forms.TextBox
- Friend WithEvents chkMultiline As System.Windows.Forms.CheckBox
- Friend WithEvents chkIgnoreCase As System.Windows.Forms.CheckBox
- Friend WithEvents txtResult As System.Windows.Forms.TextBox
- Friend WithEvents chkShowGroups As System.Windows.Forms.CheckBox
- Friend WithEvents chkShowCaptures As System.Windows.Forms.CheckBox
- Friend WithEvents lblResultCount As System.Windows.Forms.Label
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.btnSplit = New System.Windows.Forms.Button()
- Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
- Me.Label4 = New System.Windows.Forms.Label()
- Me.txtResult = New System.Windows.Forms.TextBox()
- Me.Label2 = New System.Windows.Forms.Label()
- Me.Label3 = New System.Windows.Forms.Label()
- Me.btnFind = New System.Windows.Forms.Button()
- Me.btnReplace = New System.Windows.Forms.Button()
- Me.btnLoad = New System.Windows.Forms.Button()
- Me.chkShowGroups = New System.Windows.Forms.CheckBox()
- Me.chkMultiline = New System.Windows.Forms.CheckBox()
- Me.txtSource = New System.Windows.Forms.TextBox()
- Me.txtRegex = New System.Windows.Forms.TextBox()
- Me.txtReplace = New System.Windows.Forms.TextBox()
- Me.Label1 = New System.Windows.Forms.Label()
- Me.chkIgnoreCase = New System.Windows.Forms.CheckBox()
- Me.chkShowCaptures = New System.Windows.Forms.CheckBox()
- Me.lblResultCount = New System.Windows.Forms.Label()
- Me.SuspendLayout()
- '
- 'btnSplit
- '
- Me.btnSplit.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
- Me.btnSplit.Location = New System.Drawing.Point(288, 72)
- Me.btnSplit.Name = "btnSplit"
- Me.btnSplit.Size = New System.Drawing.Size(64, 32)
- Me.btnSplit.TabIndex = 2
- Me.btnSplit.Text = "&Split"
- '
- 'Label4
- '
- Me.Label4.Location = New System.Drawing.Point(8, 248)
- Me.Label4.Name = "Label4"
- Me.Label4.Size = New System.Drawing.Size(72, 24)
- Me.Label4.TabIndex = 0
- Me.Label4.Text = "Results"
- '
- 'txtResult
- '
- Me.txtResult.AcceptsReturn = True
- Me.txtResult.AcceptsTab = True
- Me.txtResult.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
- Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.txtResult.Location = New System.Drawing.Point(8, 272)
- Me.txtResult.Multiline = True
- Me.txtResult.Name = "txtResult"
- Me.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Both
- Me.txtResult.Size = New System.Drawing.Size(600, 104)
- Me.txtResult.TabIndex = 4
- Me.txtResult.Text = ""
- '
- 'Label2
- '
- Me.Label2.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
- Me.Label2.Location = New System.Drawing.Point(376, 16)
- Me.Label2.Name = "Label2"
- Me.Label2.Size = New System.Drawing.Size(248, 24)
- Me.Label2.TabIndex = 0
- Me.Label2.Text = "Replace Pattern"
- '
- 'Label3
- '
- Me.Label3.Location = New System.Drawing.Point(8, 112)
- Me.Label3.Name = "Label3"
- Me.Label3.Size = New System.Drawing.Size(248, 24)
- Me.Label3.TabIndex = 0
- Me.Label3.Text = "Text to be parsed"
- '
- 'btnFind
- '
- Me.btnFind.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
- Me.btnFind.Location = New System.Drawing.Point(288, 40)
- Me.btnFind.Name = "btnFind"
- Me.btnFind.Size = New System.Drawing.Size(64, 32)
- Me.btnFind.TabIndex = 2
- Me.btnFind.Text = "&Find"
- '
- 'btnReplace
- '
- Me.btnReplace.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
- Me.btnReplace.Location = New System.Drawing.Point(608, 40)
- Me.btnReplace.Name = "btnReplace"
- Me.btnReplace.Size = New System.Drawing.Size(80, 32)
- Me.btnReplace.TabIndex = 2
- Me.btnReplace.Text = "&Replace"
- '
- 'btnLoad
- '
- Me.btnLoad.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
- Me.btnLoad.Location = New System.Drawing.Point(608, 136)
- Me.btnLoad.Name = "btnLoad"
- Me.btnLoad.Size = New System.Drawing.Size(80, 32)
- Me.btnLoad.TabIndex = 2
- Me.btnLoad.Text = "&Load"
- '
- 'chkShowGroups
- '
- Me.chkShowGroups.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
- Me.chkShowGroups.CheckAlign = System.Drawing.ContentAlignment.TopLeft
- Me.chkShowGroups.Checked = True
- Me.chkShowGroups.CheckState = System.Windows.Forms.CheckState.Checked
- Me.chkShowGroups.Font = New System.Drawing.Font("Microsoft Sans Serif", 10!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.chkShowGroups.Location = New System.Drawing.Point(616, 272)
- Me.chkShowGroups.Name = "chkShowGroups"
- Me.chkShowGroups.Size = New System.Drawing.Size(80, 40)
- Me.chkShowGroups.TabIndex = 5
- Me.chkShowGroups.Text = "Show Groups"
- Me.chkShowGroups.TextAlign = System.Drawing.ContentAlignment.TopLeft
- '
- 'chkMultiline
- '
- Me.chkMultiline.Location = New System.Drawing.Point(144, 80)
- Me.chkMultiline.Name = "chkMultiline"
- Me.chkMultiline.Size = New System.Drawing.Size(88, 24)
- Me.chkMultiline.TabIndex = 3
- Me.chkMultiline.Text = "&Multiline"
- '
- 'txtSource
- '
- Me.txtSource.AcceptsReturn = True
- Me.txtSource.AcceptsTab = True
- Me.txtSource.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.txtSource.Location = New System.Drawing.Point(8, 136)
- Me.txtSource.Multiline = True
- Me.txtSource.Name = "txtSource"
- Me.txtSource.ScrollBars = System.Windows.Forms.ScrollBars.Both
- Me.txtSource.Size = New System.Drawing.Size(600, 104)
- Me.txtSource.TabIndex = 4
- Me.txtSource.Text = ""
- '
- 'txtRegex
- '
- Me.txtRegex.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.txtRegex.Location = New System.Drawing.Point(8, 40)
- Me.txtRegex.Name = "txtRegex"
- Me.txtRegex.Size = New System.Drawing.Size(280, 26)
- Me.txtRegex.TabIndex = 1
- Me.txtRegex.Text = ""
- '
- 'txtReplace
- '
- Me.txtReplace.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
- Me.txtReplace.Location = New System.Drawing.Point(376, 40)
- Me.txtReplace.Name = "txtReplace"
- Me.txtReplace.Size = New System.Drawing.Size(232, 26)
- Me.txtReplace.TabIndex = 1
- Me.txtReplace.Text = ""
- '
- 'Label1
- '
- Me.Label1.Location = New System.Drawing.Point(8, 16)
- Me.Label1.Name = "Label1"
- Me.Label1.Size = New System.Drawing.Size(248, 24)
- Me.Label1.TabIndex = 0
- Me.Label1.Text = "Regular Expression Pattern"
- '
- 'chkIgnoreCase
- '
- Me.chkIgnoreCase.Location = New System.Drawing.Point(8, 80)
- Me.chkIgnoreCase.Name = "chkIgnoreCase"
- Me.chkIgnoreCase.Size = New System.Drawing.Size(120, 24)
- Me.chkIgnoreCase.TabIndex = 3
- Me.chkIgnoreCase.Text = "&Ignore case"
- '
- 'chkShowCaptures
- '
- Me.chkShowCaptures.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
- Me.chkShowCaptures.CheckAlign = System.Drawing.ContentAlignment.TopLeft
- Me.chkShowCaptures.Font = New System.Drawing.Font("Microsoft Sans Serif", 10!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.chkShowCaptures.Location = New System.Drawing.Point(616, 336)
- Me.chkShowCaptures.Name = "chkShowCaptures"
- Me.chkShowCaptures.Size = New System.Drawing.Size(80, 40)
- Me.chkShowCaptures.TabIndex = 5
- Me.chkShowCaptures.Text = "Show Captures"
- Me.chkShowCaptures.TextAlign = System.Drawing.ContentAlignment.TopLeft
- '
- 'lblResultCount
- '
- Me.lblResultCount.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.lblResultCount.ForeColor = System.Drawing.Color.RoyalBlue
- Me.lblResultCount.Location = New System.Drawing.Point(88, 248)
- Me.lblResultCount.Name = "lblResultCount"
- Me.lblResultCount.Size = New System.Drawing.Size(520, 24)
- Me.lblResultCount.TabIndex = 0
- '
- 'Form1
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
- Me.ClientSize = New System.Drawing.Size(696, 381)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblResultCount, Me.chkShowCaptures, Me.chkShowGroups, Me.txtResult, Me.Label4, Me.btnLoad, Me.txtSource, Me.Label3, Me.btnReplace, Me.chkMultiline, Me.chkIgnoreCase, Me.btnSplit, Me.btnFind, Me.txtReplace, Me.txtRegex, Me.Label2, Me.Label1})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.MinimumSize = New System.Drawing.Size(704, 408)
- Me.Name = "Form1"
- Me.Text = "Regex Tester"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- ' ask the user which file should be loaded
- Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
- With openFileDialog1()
- .DefaultExt = "TXT"
- .CheckFileExists = True
- .Filter = "(Text files (*.txt)|*.txt|All files (*.*)|*.*"
- .FilterIndex = 0
- If .ShowDialog = DialogResult().OK Then
- ' Read the text file
- txtSource().Text = FileText(.FileName)
- End If
- End With
- End Sub
-
- ' Find matches
-
- Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
- ' find matches
- Dim re As Regex
- Dim reOpt As RegexOptions = GetRegexOptions()
-
- Try
- ' attempt to create the RegEx object
- re = New Regex(txtRegex().Text, reOpt)
- Catch ex As Exception
- ' if any error, display it and exit
- MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End Try
-
- ' get the collection of results
- Dim mc As MatchCollection = re.Matches(txtSource().Text)
- ' display number of matches
- lblResultCount.Text = "Found " & mc.Count.ToString & " matches."
- txtResult.Clear()
-
- Dim m As Match
- For Each m In mc
- ShowRes("'" & m.Value & "' at index " & m.Index)
- If m.Groups.Count > 1 And chkShowGroups.Checked = True Then
- Dim g As Group, i As Integer
- ' skip the 0th group, which is the entire Match object
- For i = 1 To m.Groups.Count - 1
- ' Get a reference to the corresponding group.
- g = m.Groups(i)
- ShowRes(String.Format(" group({0}) = '{1}'", i, g.Value))
- If chkShowCaptures.Checked = True Then
- ' Get the capture collection for this group.
- Dim cc As CaptureCollection = g.Captures
- ' Display information on each capture.
- Dim c As Capture
- For Each c In cc
- ShowRes(String.Format(" capture '{0}' at index {1}", c.Value, c.Index))
- Next
- End If
- Next
- End If
- Next
- End Sub
-
- ' retrieve RegEx options
-
- Function GetRegexOptions() As RegexOptions
- ' retrieve options
- If chkIgnoreCase().Checked Then GetRegexOptions = RegexOptions.IgnoreCase
- If chkMultiline().Checked Then
- GetRegexOptions = GetRegexOptions Or RegexOptions.Multiline
- End If
- End Function
-
- ' Read the contents of a text file.
-
- Function FileText(ByVal path As String) As String
- ' Open a file stream and define a stream reader.
- Dim fs As New FileStream(path, FileMode.Open)
- Dim sr As New StreamReader(fs)
- ' Read the entire contents of this file.
- FileText = sr.ReadToEnd
- ' Clean-up code.
- sr.Close()
- fs.Close()
- End Function
-
- ' display data in the Results pane
-
- Sub ShowRes(ByVal msg As String, Optional ByVal ClearIt As Boolean = False)
- If ClearIt Then txtResult().Clear()
- txtResult().AppendText(msg & ControlChars.CrLf)
- End Sub
-
- ' replace pattern
-
- Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click
- ' very concise syntax
- Try
- txtResult().Text = Regex.Replace(txtSource().Text, txtRegex().Text, txtReplace().Text, GetRegexOptions())
- Catch ex As Exception
- ' if any error, display it
- MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- End Try
- End Sub
-
- ' split the source
-
- Private Sub btnSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSplit.Click
- Dim re As Regex
-
- Try
- re = New Regex(txtRegex().Text, GetRegexOptions())
- Catch ex As Exception
- ' if any error, display it
- MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- End Try
-
- ' split the input string
- Dim sArr() As String = re.Split(txtSource().Text)
- ShowRes("Found " & sArr.Length & " items.", True)
-
- Dim s As String
- For Each s In sArr
- ShowRes(s)
- Next
- End Sub
-
- End Class
-